import {defineStore} from "pinia";
import {reactive} from "vue";
import axios from "axios";
import {nanoid} from "nanoid";

// 选项式写法
/*
export const useLoveTalkStore = defineStore('loveTalk', {
    state() {
        return {
            list: JSON.parse(localStorage.getItem('talkList')) as string || [],
            a: 'a',
            b: 'b'
        }
    },
})*/

// 组合式写法
export const useLoveTalkStore = defineStore('loveTalk', () => {
    let list = reactive(JSON.parse(localStorage.getItem('talkList') as string)  || [])

    async function getLoveTalk() {
        let {data:{content}} = await axios.get('https://api.uomg.com/api/rand.qinghua');
        list.unshift({id: nanoid(), title: content})
    }

    return {list, getLoveTalk}
})
